package all.com.jinyongqunxiax.model.mod;

import com.randy.fundation.JxAssert;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import all.com.jinyongqunxiax.BuildConfig;
import androidx.annotation.NonNull;
import jy.game.components.event.triggers.JyGlobalTrigger;
import jy.game.tools.BasePojoManager;

public class TriggerManager {
    public Map<String, JyGlobalTrigger> map  = new HashMap<>();
    public List<JyGlobalTrigger>        list = new ArrayList<>();
    String xmlPath;

    public TriggerManager(@NonNull String xmlPath) {
        this.xmlPath = xmlPath;
    }

    BasePojoManager<JyGlobalTrigger> manager;

    public String load() {
        map.clear();
        list.clear();

        manager = BasePojoManager.build(xmlPath, JyGlobalTrigger.class);
        list = manager.datas;
        if (!manager.success()) {
            return manager.message;
        }
        StringBuilder message = new StringBuilder(getClass().getSimpleName());

        for (JyGlobalTrigger item : list) {
            if (map.containsKey(item.story)) {
                message.append("发现重复:").append(item.story);
                continue;
            }
            map.put(item.story, item);
        }


        return manager.message;
    }
//    public String load() {
//        try {
//            map.clear();
//            list.clear();
//
//            Serializer serializer = new Persister();
//
//            File file = new File(xmlPath);
//            Root root = serializer.read(Root.class, file);
//            assert !BuildConfig.DEBUG || !root.models.isEmpty();
//
//            list = root.models;
//            for (Model model : list) {
//                map.put(model.name, model);
//            }
//            if (BuildConfig.DEBUG) {
//                assert map.entrySet().size() == list.size();
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            return e.getMessage();
//        }
//        return "";
//    }
//
//
//    static public class Root {
//        @ElementList(inline = true) List<Model> models;
//    }
//
//    /**
//     * <trigger story="mainStory_神秘剑客">
//     * <condition type="level_greater_than" value="主角#30"/>
//     * <condition type="friendCount" value="30"/>
//     * <condition type="should_finish" value="mainStory_黑暗的阴影1"/>
//     * </trigger>
//     */
//    @ wx.xml.simpleframework.xml.Root(name = "trigger")
//    static public class Model {
//        public @Attribute(name = "story")  String          name;
//        public @ElementList(inline = true) List<JyCondition> conditions;
//    }
//
//    @ wx.xml.simpleframework.xml.Root(name = "condition")
//    static public class JyCondition {
//        public @Attribute String type;
//        public @Attribute String value;
//    }
}
